<?php

/*
 * @Package Softpin Order Manager
 * @Subpackage : sim Manager
 * Create by : thuc.lehuy@gmail.com
 */
// No direct access
defined('_JEXEC') or die('Restricted access');
//import view parent class
jimport('joomla.application.component.view');

class SimsViewSims extends JView {

    function display($tpl = null) {
        JToolBarHelper::title(JText::_('Quản lý Sim'), 'generic.png');
        if ($this->authen()) {
            JToolBarHelper::addNewX();
            JToolBarHelper::deleteList();
            JToolBarHelper::editListX();
            JToolBarHelper::publish();
            JToolBarHelper::unpublish();
        }
        JToolBarHelper::help('screen.banners');
        global $mainframe, $option;

        $db = & JFactory::getDBO();

        $client = JRequest::getWord('filter_client', 'site');

        $filter_order = $mainframe->getUserStateFromRequest("$option.$client.filter_order", 'filter_order', 'p.sim_id', 'cmd');
        $filter_order_Dir = $mainframe->getUserStateFromRequest("$option.$client.filter_order_Dir", 'filter_order_Dir', '', 'word');
        $filter_state = $mainframe->getUserStateFromRequest("$option.$client.filter_state", 'filter_state', '', 'word');
        $filter_type  = $mainframe->getUserStateFromRequest( "$option.$client.filter_type", 'filter_type', 0 ,'cmd' );
        $filter_type_provider   = $mainframe->getUserStateFromRequest( "$option.$client.filter_type_provider", 'filter_type_provider', 0 ,'cmd' );
        $filter_type_admin  = $mainframe->getUserStateFromRequest( "$option.$client.filter_type_admin", 'filter_type_admin', 0 ,'cmd' );

        //
        $search = $mainframe->getUserStateFromRequest("$option.$client.search", 'search', '', 'string');
        if (strpos($search, '"') !== false) {
            $search = str_replace(array('=', '<'), '', $search);
        }
        $search = JString::strtolower($search);
        $limit = $mainframe->getUserStateFromRequest('global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int');
        $limitstart = $mainframe->getUserStateFromRequest($option . '.limitstart', 'limitstart', 0, 'int');

        $where = '';

        // used by filter
        if ($filter_type != 0) {
            $where[] = 'p.categori_id = ' . $db->Quote($filter_type);
        }
        // used by filter
        if ($filter_type_provider != 0) {
            $where[] = 'p.provider_id = ' . $db->Quote($filter_type_provider);
        }
        // used by filter
        if ($filter_type_admin != 0) {
            $where[] = 'p.admin_id = ' . $db->Quote($filter_type_admin);
        }
        if ($search) {
            $where[] = 'LOWER( p.sim_numb_absolute ) LIKE ' . $db->Quote('%' . $db->getEscaped(ereg_replace("[^0-9]", "", $search ), true) . '%', false);
        }
        if ($filter_state) {
            if ($filter_state == 'P') {
                $where[] = 'p.del_flag = 0';
            } else if ($filter_state == 'U') {
                $where[] = 'p.del_flag = 1';
            }
        }

        $where = ( $where? ' WHERE ' . implode(' AND ', $where) : '' );
        // sanitize $filter_order
        if (!in_array($filter_order, array('p.sim_id', 'admin_name', 'provider_name', 'categori_name', 'p.price', 'p.sim_numb', 'p.curency', 'p.created_at'))) {
            $filter_order = 'p.sim_id';
        }

        if (!in_array(strtoupper($filter_order_Dir), array('ASC', 'DESC'))) {
            $filter_order_Dir = '';
        }

        
        $orderby = ' ORDER BY ' . $filter_order . ' ' . $filter_order_Dir . ', p.sim_id ASC';
        // get the total number of records
        $query = 'SELECT COUNT(*)'
                . ' FROM #__sim AS p'
                . $where
        ;
        $db->setQuery($query);
        $total = $db->loadResult();

        jimport('joomla.html.pagination');
        $pagination = new JPagination($total, $limitstart, $limit);

        $query = 'SELECT p.*, pr.provider_name, c.categori_name, a.admin_name'
                . ' FROM #__sim AS p'
                . ' INNER JOIN #__sim_provider AS pr ON pr.provider_id = p.provider_id and pr.del_flag = 0'
                . ' INNER JOIN #__sim_categories AS c ON c.categori_id = p.categori_id and c.del_flag = 0'
                . ' INNER JOIN #__sim_admin AS a ON a.admin_id = p.admin_id and a.del_flag = 0'
                . $where
                . ' GROUP BY p.sim_id'
                . $orderby
        ;
        $db->setQuery($query, $pagination->limitstart, $pagination->limit);
        $rows = $db->loadObjectList();
        if ($db->getErrorNum()) {
            echo $db->stderr();
            return false;
        }


        // get list of category for dropdown filter
        $query = 'SELECT categori_id AS value, categori_name AS text'
                . ' FROM #__sim_categories'
                . ' WHERE del_flag = 0';
        $types[] = JHTML::_('select.option', 0, '- ' . JText::_('Chọn loại') . ' -');
        $db->setQuery($query);
        $types = array_merge($types, $db->loadObjectList());
        
        $lists['type'] = JHTML::_('select.genericlist', $types, 'filter_type', 'class="inputbox" size="1" onchange="document.adminForm.submit( );"', 'value', 'text', $filter_type);
         // get list of provider for dropdown filter
        $query = 'SELECT provider_id AS value, provider_name AS text'
                . ' FROM #__sim_provider'
                . ' WHERE del_flag = 0';
        
        $types = null;
        $types[] = JHTML::_('select.option', 0, '- ' . JText::_('Chọn mạng') . ' -');
        $db->setQuery($query);
        $types = array_merge($types, $db->loadObjectList());
        
        $lists['type_provider'] = JHTML::_('select.genericlist', $types, 'filter_type_provider', 'class="inputbox" size="1" onchange="document.adminForm.submit( );"', 'value', 'text', $filter_type_provider);
         // get list of admin for dropdown filter
        $query = 'SELECT admin_id AS value, admin_name AS text'
                . ' FROM #__sim_admin'
                . ' WHERE del_flag = 0';

        $types = null;
        $types[] = JHTML::_('select.option', 0, '- ' . JText::_('Chọn kho') . ' -');
        $db->setQuery($query);
        $types = array_merge($types, $db->loadObjectList());

        $lists['type_admin'] = JHTML::_('select.genericlist', $types, 'filter_type_admin', 'class="inputbox" size="1" onchange="document.adminForm.submit( );"', 'value', 'text', $filter_type_admin);
        // state filter
        $lists['state'] = JHTML::_('grid.state', $filter_state);


        // table ordering
        $lists['order_Dir'] = $filter_order_Dir;
        $lists['order'] = $filter_order;

        // search filter
        $lists['search']= $search;

        $this->assign('client',$client);
        $this->assignRef('total', $total);
        $this->assignRef('user',JFactory::getUser());
        $this->assignRef('lists',$lists);
        $this->assignRef('items',$rows);
        $this->assignRef('pagination',$pagination);

        parent::display($tpl);

    }

    /*
     * Authentication
     * Manager can not edit,new,publish,unpublish
     */

    function authen() {
        $user = JFactory::getUser();
        if ($user->get('gid') < 24) {
            return 0;
        } else {
            return 1;
        }
    }

}

